package com.config;

import com.lanjie.LoginFilter;
import com.lanjie.MySessionListener;
import com.pojo.Account;
import com.pojo.Teacher;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

@Configuration //相当于 spring 的配置文件  applicationContext.xml
//applicationContext.xml
//  <bean id="getAccount" class="com.pojo.Account"/>
public class MyConfig {

    @Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean f = new FilterRegistrationBean();
        LoginFilter lf = new LoginFilter();
        HashMap map = new HashMap();
        map.put("age","22");
        f.setInitParameters(map);
        f.setFilter(lf); // 设置自定义的过滤器
        f.setOrder(1);  //如果有多个filter 可以设置过滤器加载顺序 值越小越先加载 >=0
        f.setName("loginfilter"); //<filter-name> 指定名称
        Collection c = new ArrayList();
        ((ArrayList)c).add(0,"/*");
        f.setUrlPatterns(c);  //指定url
        return f;
    }


    /*@Bean
    public FilterRegistrationBean myFilter1(){
        FilterRegistrationBean f = new FilterRegistrationBean();
        LoginFilter lf = new LoginFilter();
        f.setFilter(lf); // 设置自定义的过滤器
        f.setOrder(2);  //如果有多个filter 可以设置过滤器加载顺序 值越小越先加载 >=0
        f.setName("loginfilter"); //<filter-name> 指定名称
        Collection c = new ArrayList();
        ((ArrayList)c).add(0,"/*");
        f.setUrlPatterns(c);  //指定url
        return f;
    }*/


    /*@Bean
    public ServletListenerRegistrationBean tianjiajianting(){
        ServletListenerRegistrationBean sl = new ServletListenerRegistrationBean();
        sl.setListener(new MySessionListener());
        return sl;
    }*/






    @Bean
    public Account getAccount(){
        System.out.println("执行了 getAccount 方法");
        Account acc = new Account();
        acc.setName("xxx");
        acc.setAid(11);
        return acc;
    }

    @Bean
    public Teacher getOneTeacher(){
        Teacher t = new Teacher();
        t.setName("xxxxxxx");
        t.setAge(33);
        return t;
    }
}
